<?php

/**
 * 漏操作查询
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
//查询订单商品生产类
quanli_require('module/base/bill/billGoodsProduct.class.php');
class ajax_billGoodsOperateForget{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //列表
                    $this->productionList();
                    break;
                case 2: //审核
                    $this->checkGoods();
                    break;
            }
        }
    }

    /*
     * 列表
     *
     * */
    public function productionList () {
        global $G_PGLOB;
        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['cityID']) && is_numeric($_REQUEST['cityID']) && isset( $_REQUEST['distribution_id']) && is_numeric($_REQUEST['distribution_id'])){
            $_REQUEST['cityID'] = $_REQUEST['cityID'];
            $_REQUEST['distribution_id'] = $_REQUEST['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['cityID']) && is_numeric($_REQUEST['cityID']) && !isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

        }

        $billGoodsOperateForget = new billGoodsProduct();
        //查找订单商品生产表数据
        $bill_array = $billGoodsOperateForget->searchBillGoodsOperateForget( $_REQUEST ) ;


        //查询结果
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $bill_array ) {
            foreach ($bill_array as &$goodsInfo) {
                $bill_goods = new entity('bill_goods');
                $bill_goods->bill_goods_no = $goodsInfo['bill_goods_no'] ;


                if ( $G_PGLOB->DBCA->fetch( $bill_goods) ){

//                    if( $goodsInfo['operate_record'] == '' ){
//                        $operatorString = '<a href="javascript: void(0);" onclick=bill_goods.checkGoods("'.$goodsInfo['bill_goods_no'].'",'.$goodsInfo['forget_operate_flow'].') >审核</a>';
//                    }else{
//                        $operatorString = '';
//                    }
                    //$bill_no = $bill_goods->bill_no;

                    $bill_master = new entity('bill_master');
                    $bill_master->bill_no = $bill_goods->bill_no;
                    if ( !$G_PGLOB->DBCA->fetch( $bill_master ) ){
                        returnJsonData('未找到改订单','400');
                    }
                    if( $bill_master->bill_status == 10){
                        $operate_record = '';
                        $operatorString = '';
                    }elseif( $bill_goods->is_warehouse == 1 && $goodsInfo['operate_record']== ''){
                        $operate_record = '已完成';
                        $operatorString = '';
                    }else if( $bill_goods->is_warehouse == 0 && $goodsInfo['operate_record']== ''){
                        $operate_record = '';
                        $operatorString = '<a href="javascript: void(0);" onclick=bill_goods.checkGoods("'.$goodsInfo['bill_goods_no'].'",'.$goodsInfo['forget_operate_flow'].') >审核</a>';
                    }else{
                        $operate_record = $goodsInfo['operate_record'];
                        $operatorString = '';
                    }

                }

                if( $goodsInfo['forget_operate_flow'] == 1){
                    $forget_operate_flow_name = '上车扫描';
                }else{
                    $forget_operate_flow_name = '质检入库、上车扫描';
                }
                $result_array['rows'][] = array(
                    'id' => $goodsInfo['bill_goods_no'],
                    'cell' => array(
                        $operatorString,
                        //$goodsInfo['bill_code'],
                        '<a href="javascript: void(0);" onclick="bill_goods.billview(\''.$goodsInfo['bill_no'].'\');">'.$goodsInfo['bill_code'].'</a>',
                        $forget_operate_flow_name,
                        $goodsInfo['distribution_name'],
                        $goodsInfo['send_date'],
                        $operate_record,
                        $goodsInfo['forget_check_operator'],
                        $goodsInfo['forget_check_operate_time']

                    )
                );
            }
        }

        echo json_encode($result_array);

    }

    /*
     * 审核漏操作订单商品(补全前面的操作流程)
     */
    public function checkGoods(){
        global $G_PGLOB;

        if(!isset($_REQUEST['bill_goods_no']) || empty($_REQUEST['bill_goods_no'])){
            returnJsonData('缺少参数','400');
        }

        if(!isset($_REQUEST['forget_operate_flow']) || !is_numeric($_REQUEST['forget_operate_flow'])){
            returnJsonData('缺少参数','400');
        }

        try {
            //开启事务
            $G_PGLOB->DBCA->beginTransaction();

            $bill_goods = new entity('bill_goods');
            $bill_goods->bill_goods_no = $_REQUEST['bill_goods_no'] ;
            if ( !$G_PGLOB->DBCA->fetch( $bill_goods) ){
                throw new Exception( '未找到该订单', 400);
            }

            if( $bill_goods->print_num == 0){
                throw new Exception( '该订单商品还未生产，无法进行审核操作', 400);
            }

            if( $bill_goods->is_warehouse == 1){
                throw new Exception( '该订单已上车扫描，请重新加载', 400);
            }else{


                /**
                 * =====================================================
                 * 订单商品生产表bill_goods_product添加出库信息start
                 * =====================================================
                 */
                $bill_goods_product = new entity('bill_goods_product');
                $bill_goods_product ->bill_goods_no = $_REQUEST['bill_goods_no'];
                if($G_PGLOB->DBCA->fetch($bill_goods_product)){
                    $bill_goods_product->is_voucher = 1; //入库状态
                    $bill_goods_product->voucher_operator = $G_PGLOB->currentUserID;
                    $bill_goods_product->voucher_time = $G_PGLOB->sys_datetime;

                    $bill_goods_product->is_warehouse = 1; //出库状态
                    $bill_goods_product->warehouse_operator = $G_PGLOB->currentUserID;
                    $bill_goods_product->warehouse_time = $G_PGLOB->sys_datetime;
                    $G_PGLOB->DBCA->save($bill_goods_product);
                }



                /**
                 * ========================================================
                 * 订单商品生产表bill_goods_product添加出库信息end
                 * ========================================================
                 */


                /**
                 * ======================================================
                 * 订单商品表bill_goods添加出库信息start
                 * ======================================================
                 */
                $bill_goods = new entity('bill_goods');
                $bill_goods ->bill_goods_no = $_REQUEST['bill_goods_no'];
                if(!$G_PGLOB->DBCA->fetch($bill_goods)){
                    throw new Exception( '订单商品不存在' , 404 );
                }

                $bill_goods_product->is_voucher = 1; //入库状态
                $bill_goods_product->voucher_operator = $G_PGLOB->currentUserID;
                $bill_goods_product->voucher_time = $G_PGLOB->sys_datetime;

                $bill_goods->is_warehouse = 1; //出库状态
                $bill_goods->warehouse_operator = $G_PGLOB->currentUserID;
                $bill_goods->warehouse_time = $G_PGLOB->sys_datetime;
                $G_PGLOB->DBCA->save($bill_goods);
                /**
                 * ========================================
                 * 订单商品表bill_goods添加出库信息end
                 * ========================================
                 */


                /**
                 * ========================================
                 * 改变订单主表订单状态start
                 * ========================================
                 */
                $bill_master = new entity('bill_master');
                $bill_master->bill_no = $bill_goods->bill_no;
                if (!$G_PGLOB->DBCA->fetch($bill_master)){
                    throw new Exception( '订单信息不存在' , 404 );
                }
                //判断订单商品总数量 与 订单商品出库数量 相等 订单商品“已出库”
                if( $bill_master->bill_totalAmount > $bill_master->bill_out_warehouse_amount ){
                    $bill_master->bill_out_warehouse_amount = $bill_master->bill_out_warehouse_amount + 1; //出库订单商品总数量+1
                }

                if ($bill_master->bill_totalAmount == $bill_master->bill_out_warehouse_amount && ($bill_master->bill_status == 3 || $bill_master->bill_status == 2)) {
                    $bill_master->bill_status = 4; //状态为“已出库”
                }

                $G_PGLOB->DBCA->save($bill_master);

                /**
                 * ========================================
                 * 改变订单主表订单状态end
                 * ========================================
                 */

                /**
                 * ========================================
                 * 修改漏操作表休息start
                 * ========================================
                 */
                $bill_goods_operate_forget = new entity('bill_goods_operate_forget');
                $bill_goods_operate_forget ->bill_goods_no = $_REQUEST['bill_goods_no'];
                if(!$G_PGLOB->DBCA->fetch($bill_goods_operate_forget)){
                    throw new Exception( '漏操作订单商品不存在' , 404 );
                }

                $currentTime = $G_PGLOB->sys_datetime;
                $operater = $G_PGLOB->currentUserID;
                if( $_REQUEST['forget_operate_flow'] == 1){
                    $info = '上车扫描';
                }else{
                    $info = '质检入库、上车扫描';
                }
                $bill_goods_operate_forget->operate_record = $currentTime.' '.$operater.' '.'审核-'.$info; //入库状态
                $bill_goods_operate_forget->forget_check_operator = $operater;
                $bill_goods_operate_forget->forget_check_operate_time = $currentTime;
                $G_PGLOB->DBCA->save( $bill_goods_operate_forget );
                /**
                 * ========================================
                 * 修改漏操作表休息end
                 * ========================================
                 */

                //提交事务
                $G_PGLOB->DBCA->commit();

                returnJsonData( '审核成功', 200 );
            }

        } catch (Exception $e){

            //回滚事务
            $G_PGLOB->DBCA->rollBack();

            if ($e->getCode() > 0) {
                returnJsonData( $e->getMessage(),$e->getCode());
                return;
            } else {
                returnJsonData( '系统错误，请与管理员联系',500);
                return;
            }

        }

    }

}
